// pages/home/home.js

import {Theme} from "../../models/theme";
import {Banner} from "../../models/banner";
import {Category} from "../../models/category";
import {Activity} from "../../models/activity";
import {SpuPaging} from "../../models/spu-paging";

Page({

    /**
     * 页面的初始数据:代码易读更清晰，容易维护
     */
    data: {
        themeA: null,
        themeE: null,
        themeF: null,
        themeH: null,
        bannerB: null,
        bannerG: null,
        grid: [],
        activityD: null,
        themeESpu: [],
        spuPaging: null,
        loadingType: 'loading'
    },


    onLoad(options) {
        // console.log({}.count) //undefined
        this.initAllData().then()
        this.initBottomSpuList().then()
    },

    async initBottomSpuList() {
        const paging = SpuPaging.getLatestPaging()
        this.data.spuPaging = paging
        const data = await paging.getMoreData()
        if (!data) {
            return
        }
        wx.lin.renderWaterFlow(data.items)
    },

    async initAllData() {
        // const themeA = await Theme.getHomeLocationA()
        const theme = new Theme()
        await theme.getThemes()
        const themeA = theme.getHomeLocationA()
        const themeE = theme.getHomeLocationE()
        let themeESpu = []
        if (themeE.online) {
            const data = await theme.getHomeLocationESpu()
            if (data) {
                themeESpu = data.spu_list.slice(0, 8)
            }
        }
        const themeF = theme.getHomeLocationF()
        const themeH = theme.getHomeLocationH()

        const bannerB = await Banner.getHomeLocationB()
        const bannerG = await Banner.getHomeLocationG()
        const grid = await Category.getHomeLocationC()
        const activityD = await Activity.getHomeLocationD()


        // undefinde / null可以被用于setData，但是undefined / null不能定义属性，定义属性就会报错，整个页面都加载不出来
        // const hel = undefined
        // const helx = hel.data
        // console.log(helx)

        this.setData({
            themeA,
            themeE,
            themeF,
            themeH,
            bannerB,
            bannerG,
            grid,
            activityD,
            themeESpu
        })
    },

    onPullDownRefresh: function () {

    },

    async onReachBottom() {
        const data = await this.data.spuPaging.getMoreData()
        if (!data) {
            return
        }
        wx.lin.renderWaterFlow(data.items) // lin-water-flow 会自动进行累加计算，不需要我们关心，所以就传入当前获取的数据就行
        if (!data.moreData) {
            this.setData({
                loadingType: 'end'
            })
        }
    },

    onShareAppMessage: function () {

    }
})